<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <%@ page import="goods.*"%>
    <%@ page import="java.util.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page import="java.net.URLEncoder" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@ include file="/Dmyna/admin/head.jsp" %>

<%
	String gs_code = request.getParameter("gs_code");
	//System.out.println("gs_code"+gs_code);
	GoodsManager goodsManager = GoodsManager.getInstance();
	GoodsItem goodsItem = goodsManager.select2(gs_code);
%>

<%
String optionColor[] = {"블렉","화이트","그레이","카키","핑크","레드","옐로우","퍼플","스카이"};
String optionSize[] = {"FREE","S","M","L","XL","XXL","220mm","230mm","240mm","260mm","270mm"};

String checkedColor[] = new String[optionColor.length];
String checkedSize[] = new String[optionSize.length];

StringTokenizer tokColor = new StringTokenizer(goodsItem.getColor_code(), "|");
StringTokenizer tokSize = new StringTokenizer(goodsItem.getSize_code(), "|");
String tokTmpStr = "";

/*
배열 초기화 - 없어도 가능
for(int i=0; i<checkedColor.length; i++){
	checkedColor[i] = "";

}

for(int i=0; i<checkedSize.length; i++){
	checkedSize[i] = "";
}
*/

while (tokColor.hasMoreTokens()){
	tokTmpStr = tokColor.nextToken();
	for(int i=0; i<checkedColor.length; i++){
		if(optionColor[i].equals(tokTmpStr)){
			checkedColor[i] = "checked";			
		}
	}
}

while (tokSize.hasMoreTokens()){
	tokTmpStr = tokSize.nextToken();
	for(int i=0; i<checkedSize.length; i++){
		if(optionSize[i].equals(tokTmpStr)){
			checkedSize[i] = "checked";			
		}
	}
}

%>

<c:set var="goodsItem" value="<%=goodsItem%>" />
<c:if test="${!empty goodsItem }">
<script type="text/javascript">
function validate(form){
	
	var f = document.goods;
	var checked_color_items = 0;
	var checked_size_items = 0;

	for (var i = 0; i < f.elements.length; i++) //특정 영역을 체크 할때는 숫자로 대신 한다 예: for(i=3;i<21;i++)
	{
		if((f.elements[i].name == "color_code") && (f.elements[i].checked)) {
			checked_color_items++;
		}else if((f.elements[i].name == "size_code") && (f.elements[i].checked)) {
			checked_size_items++;
		}
	}
	
	oEditors.getById["gs_content"].exec("UPDATE_CONTENTS_FIELD", []);
	
	if (form.cate_big_code_a.value == "") {
		alert("상품분류를 선택하세요.");
		return false;
	} else if (form.gs_name.value == "") {
		alert("상품이름을 입력하세요.");
		form.gs_name.focus();
		return false;
	}else if (checked_color_items == 0) {
		alert("적어도 하나의 컬러옵션을 선택하셔야 합니다.")
		return false;
	} else if (checked_size_items == 0) {
		alert("적어도 하나의 사이즈옵션을 선택하셔야 합니다.")
		return false;
	} else if (form.gs_org_price.value == "") {
		alert("판매가를 입력하세요.");
		form.gs_org_price.focus();
		return false;
	} else if (form.gs_sale_price.value == "") {
		alert("할인가를 입력하세요.");
		form.gs_sale_price.focus();
		return false;
	} else if (form.gs_sale_chk.value == "") {
		alert("할인여부를 선택하세요.");
		return false;
	} else if (form.gs_display_chk.value == "") {
		alert("진열여부를 선택하세요.");
		return false;
	} else if (document.getElementById("gs_content").value == "") {
		alert("상품설명을 입력하세요.");
		form.gs_content.focus();
		return false;
	} 
}


function SelectValue(selectvalue) {
	var sel_id = document.getElementById('s_id');
	sel_id.value = selectvalue;
}

</script>

<script type="text/javascript" src="../../se2/js/HuskyEZCreator.js" charset="utf-8"></script>

<form action="/goodsUpdate.do" method="post" name="goods" enctype="multipart/form-data" onSubmit="return validate(this)">
<input type="hidden" name="gs_code" value="${goodsItem.gs_code }" />
<table width="900" border="1" align="center" >
		
		<tr >
			<td width="100" align="center">분류</td>
			<td>
			<select name="cate_big_code" onChange="SelectValue(this.value)" tabindex="">
			<%
			
			//System.out.println("cate_big_code"+ goodsItem.getCate_big_code() );
			GoodsCategoryModel gcatem = new GoodsCategoryModel();
			for (int i = 0; i < gcatem.getRowCount(); i++) {
				//System.out.println(gcatem.getValueAt(i, 0));
				//System.out.println(gcatem.getValueAt(i, 1));
			%>
				<option value="<%=gcatem.getValueAt(i, 0)%>" <%=goodsItem.getCate_big_code().equals(gcatem.getValueAt(i, 0))?" selected":""%> ><%=gcatem.getValueAt(i, 1) %></option>
			<%
			} 
			%>
				
			
			</select>
					
					<input type="text" id="s_id" name="cate_big_code_a" readonly="readonly" value="${goodsItem.cate_big_code }" />
					</td>
		</tr>
		<tr>
			<td align="center"><font color="red">상품코드</font></td>
			<td>
			<%
			Calendar cal = Calendar.getInstance();   // 현재 날짜 및 시간 정보를 갖고 있는 Calendar클래스의 인스턴스를 생성
			String strCalendar;
			String strCalendar1;
			int year, mon, day, ho, mi, se;
			 year = cal.get(Calendar.YEAR);
			 mon = cal.get(Calendar.MONTH)+1;  
		     day = cal.get(Calendar.DAY_OF_MONTH);
		     ho = cal.get(Calendar.HOUR_OF_DAY);
		     mi = cal.get(Calendar.MINUTE);
		     se = cal.get(Calendar.SECOND);
		     strCalendar = String.valueOf(year) + String.valueOf(mon) + String.valueOf(day);
		     strCalendar1 = String.valueOf(year) + String.valueOf(mon) + String.valueOf(day) + String.valueOf(ho) 
		             + String.valueOf(mi) + String.valueOf(se);
		    
				String middle_code = String.valueOf(goodsItem.getGs_middle_code());
				char[] charArray = middle_code.toCharArray();
				StringBuffer buff = new StringBuffer();

				int num = 4 - middle_code.length();
				int j = 0;

				for (int i = 0; i < 4; i++) {
					if (i < num) {

						buff.append("0");
					} else if (i >= num) {

						buff.append(charArray[j]);
						j++;
					}
					//System.out.println(buff.toString());
				}
				
				String time = goodsItem.getGs_reg_date();
				time = time.substring(0,10);

			%>

			
			<input type="text" name="gs_middle_code" size="40" readonly="readonly" id="gmc" value="G<%=buff.toString()%>" />&nbsp;&nbsp;(자동으로 코드가 생성 됩니다.)</td>
		</tr>
		<tr>
			<td align="center">상품이름</td>
			<td><input type="text" name="gs_name" size="40"  value="${goodsItem.gs_name }" />&nbsp;&nbsp;(100자 안으로 작성)</td>
		</tr>
		<tr>
			<td align="center">컬러옵션</td>
			<td>
				<%
				for(int i=0; i<optionColor.length; i++){
				%>
				<input type="checkbox" name="color_code" value="<%= optionColor[i]%>" <%=checkedColor[i] %>><%= optionColor[i]%>
				<%
				}
				%>
				&nbsp;&nbsp;(색상을 선택해주세요)
			</td>
		</tr>
		<tr>
			<td align="center">사이즈옵션</td>
			<td>
				<%
				for(int i=0; i<optionSize.length; i++){
				%>
				<input type="checkbox" name="size_code" value="<%= optionSize[i]%>" <%=checkedSize[i] %>><%= optionSize[i]%>
				<%
				}
				%>
				&nbsp;&nbsp;(사이즈를 선택해주세요)
			</td>
		</tr>
		<tr>
			<td align="center"><font color="red">판매가</font></td>
			<td>
			<input type="text" name="gs_org_price"  maxlength="8" size="12" style="ime-mode:disabled;"
					onKeyPress="if(event.keyCode==13){ event.returnValue=false; } 
  										else if((event.keyCode<48)||(event.keyCode>57)){ 
    									alert('숫자만 넣어주세요'); 
    									event.returnValue=false; 
  										}"
					onKeyDown="if(event.keyCode==229){ 
    									alert('숫자만 넣어주세요(한글)'); 
    									this.value='';
    								    event.returnValue=false; 
    								    }"
			value="${goodsItem.gs_org_price }" /> 
			
			<%--<input type="text" name="gs_org_price" maxlength="8" size="12" onkeydown="if(event.keyCode<48 || event.keyCode>57)event.returnValue = false;" style="ime-mode:disabled">--%>
			<%--<input type="text" name="gs_org_price" maxlength="8" size="12" style="ime-mode:disabled;"
             onPropertyChange="checkNumber()" />--%>원&nbsp;&nbsp; (원가격을 입력해주세요)</td>
		</tr>
		<tr>
			<td align="center"><font color="red">할인가</font></td>
			<td>
			<input type="text" name="gs_sale_price"  maxlength="8" size="12" style="ime-mode:disabled;"
					onKeyPress="if(event.keyCode==13){ event.returnValue=false; } 
  										else if((event.keyCode<48)||(event.keyCode>57)){ 
    									alert('숫자만 넣어주세요'); 
    									event.returnValue=false; 
  										}"
					onKeyDown="if(event.keyCode==229){ 
    									alert('숫자만 넣어주세요(한글)'); 
    									this.value='';
    								    event.returnValue=false; 
    								    }"
			value="${goodsItem.gs_sale_price }" /> 
			
			<%--<input type="text" name="gs_sale_price" maxlength="8" size="12" style="ime-mode:disabled;" onPropertyChange="checkNumber()" />--%>원&nbsp;&nbsp; (할인가격을 입력해주세요)</td>
		</tr>
		<tr>
			<td align="center">할인유무</td>
			<td><input type = "radio" name = "gs_sale_chk" value="N"  ${ goodsItem.gs_sale_chk == "N" ?  "checked": "" } >판매가&nbsp;
					<input type = "radio" name = "gs_sale_chk" value="Y" ${ goodsItem.gs_sale_chk == "Y" ?  "checked": "" }>할인가&nbsp;&nbsp; (할인 판매 유무를 설정합니다.)</td>
		</tr>
		<tr>
			<td align="center">진열여부</td>
			<td><input type = "radio" name = "gs_display_chk" value="Y" ${ goodsItem.gs_display_chk == "Y" ?  "checked": "" } >진열중&nbsp;
					<input type = "radio" name = "gs_display_chk" value="N" ${ goodsItem.gs_display_chk == "N" ?  "checked": "" }>준비중&nbsp;&nbsp; (제품 전시 여부를 설정합니다.)</td>
		</tr>
		 <tr>
			<td align="center">등록일</td>
			<td><input type="text" name="gs_reg_date" size="30" readonly="readonly" value="<%=time%>"/></td>
		</tr>
		<tr>
			<td align="center">이미지</td>
			<td><input type="file" name="gs_img" size="50" />
					<c:if test="${!empty goodsItem.gs_img }"><br />
						<img src="upload/small.${goodsItem.gs_img }" width="150" border="0" />
					</c:if>
					&nbsp;&nbsp;[권장 사이즈 400 x 400 ] </td>
		</tr>
		<tr>
			<td align="center">상세이미지</td>
			<td><input type="file" name="gs_img2" size="50" />
					<c:if test="${!empty goodsItem.gs_img2 }"><br />
						<img src="upload/detail.${goodsItem.gs_img2 }" width="150" border="0" />
					</c:if>
					&nbsp;&nbsp;[권장 사이즈 900 x 500 ] </td>
		</tr>
		<tr>
			<td align="center">상품설명</td>
			<td>
				<textarea name="gs_content" id="gs_content" rows="20" cols="145">${goodsItem.gs_content }</textarea>
			</td>
		</tr>
		<tr>
			<td colspan="2" align="center">
			<input type="submit" value="수정" /> 
			<%--<input type="submit" value="등록" onclick="javascript:validate()" />--%>
			<input type="button" value="취소" onclick="javascript:history.go(-1)" /></td>
		</tr>
</table>
</form>
</c:if>

<script type="text/javascript">

var oEditors = [];

nhn.husky.EZCreator.createInIFrame({

    oAppRef: oEditors,

    elPlaceHolder: "gs_content",

    sSkinURI: "../../se2/SmartEditor2Skin.html",

    fCreator: "createSEditor2"

});

</script>

<%@ include file="/Dmyna/admin/tail.jsp" %>

